﻿#include "loadingdialog.h"
#include "ui_loadingdialog.h"
#include <QGraphicsDropShadowEffect>

LoadingDialog* s_LoadingWidget = Q_NULLPTR;

TimerThread::TimerThread(QObject* parent) :
	QThread(parent)
{
}

void TimerThread::run()
{
	QTimer timer;
	connect(&timer, &QTimer::timeout,[&]() {
		emit SigUpdate();
    });
	timer.start(200);
	exec();
}

LoadingDialog::LoadingDialog(QWidget* parent) :
	QDialog(parent),
	ui(new Ui::LoadingDialog),
	m_pTimerThread(Q_NULLPTR)
{
	ui->setupUi(this);
	//如果需要显示任务栏对话框则删除Qt::Tool
	setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
	setAttribute(Qt::WA_TranslucentBackground, true);

	this->setFixedSize(250, 180);

	//加载Loading动画
    m_pLoadingMovie = new QMovie(":/images/resources/loading.gif");
    m_pLoadingMovie->setScaledSize(QSize(250, 180));
    ui->Gif_label->setMovie(m_pLoadingMovie);
    m_pLoadingMovie->start();

//	m_pTimerThread = new TimerThread(this);
//	connect(m_pTimerThread, &TimerThread::SigUpdate, this, &LoadingDialog::on_Process_Once, Qt::QueuedConnection);
//	m_pTimerThread->start();
}

LoadingDialog::~LoadingDialog()
{
    //m_pTimerThread->quit();
	delete ui;
}

void LoadingDialog::Loading(QWidget* parent)
{
    if(Q_NULLPTR == s_LoadingWidget)
    {
        s_LoadingWidget = new LoadingDialog(parent);
    }
    //设置界面为模态窗口
    s_LoadingWidget->setModal(true);
    s_LoadingWidget->show();
}

void LoadingDialog::ProcessEvent()
{
	QApplication::processEvents();
}

void LoadingDialog::HideLoading()
{
    if(s_LoadingWidget)
    {
        s_LoadingWidget->close();
        delete s_LoadingWidget;
        s_LoadingWidget = Q_NULLPTR;
    }
}

void LoadingDialog::on_Close_Button_clicked()
{
	this->close();
}

void LoadingDialog::on_Process_Once()
{
	QApplication::processEvents();
}
